package com.example.admin.controller;


import com.example.admin.common.Result;
import com.example.admin.entity.AdminLogin;
import com.example.admin.entity.Customer;
import com.example.admin.service.AdminService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin")
@CrossOrigin(origins = "http://localhost:8080")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @GetMapping("/getCustomerList")
    public Result<PageInfo> getCustomerList(@RequestParam(defaultValue = "1") Integer pageNumber,@RequestParam(defaultValue = "10") Integer pageSize) {

        PageHelper.startPage(pageNumber,pageSize);
       List<Customer> customerList= adminService.getCustomerList();
       return Result.success(new PageInfo(customerList));
    }

    /**
     * 修改角色
     */

    @PutMapping("/updateRole")
    public Result updateRole(@RequestBody Customer customer) {
       Result result= adminService.updateRole(customer);
       return result;
    }

    /**
     * 根据用户名查询
     */

    @GetMapping("/getCustomerInfoByName")
    public Result<Customer> getCustomerInfoByName(@RequestParam("customerName") String customerName) {

      Customer customer=  adminService.getCustomerByName(customerName);
      return Result.success(customer);

    }

    /**
     * 删除对应的角色
     */
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
       Result result= adminService.deleteById(id);
       return result;
    }

    /**
     * 登录接口
     */

    @PostMapping("/login")
    public Result login(@RequestBody AdminLogin adminLogin) {

       Result result= adminService.login(adminLogin);
       return result;

    }

    /**
     * 注册接口
     */

    @PostMapping("/register")
    public Result register(@RequestBody AdminLogin adminLogin) {
      Result result=  adminService.register(adminLogin);
      return result;


    }







}
